<template>
  <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogTitle"
      width="30%"
      @close="handleClose"
  >
      <el-form
          ref="formRef"
          :model="form"
          :rules="rules"
          label-width="100px"
      >
        <el-form-item label="角色名称" prop="rolename">
          <el-input v-model="form.rolename"  />
        </el-form-item>
        <el-form-item label="权限字符" prop="code">
          <el-input v-model="form.code" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" :row="4"/>
        </el-form-item>

      </el-form>

    <template #footer>
      <span class="dialog-footer">
      <el-button type="primary" @click="handleConfirm">确认</el-button>
      <el-button @click="handleClose"
      >取消</el-button
      >
      </span>
    </template>

  </el-dialog>
</template>

<script setup>
import {defineEmits, defineProps,ref,watch } from "vue"
import request,{getServerUrl} from "@/util/request";
import { ElMessage } from 'element-plus'

//接收属性
const props=defineProps(
    {
      id:{
        type:Number,
        default:-1,
        required:true
      },
      dialogTitle:{
        type:String,
        default:'',
        required:true
      },
      dialogVisible:{
        type:Boolean,
        default:false,
        required:true
      }
    }
)

const form=ref({
  id:-1,
  rolename:"",
  code:"",
  remark:""
})

const checkRolename = async (rule, value, callback) => {
  if(form.value.id==-1){
    const res = await request.post("sys/role/checkRoleName",
        {rolename:form.value.rolename});
    if (res.data.code==500) {
      callback(new Error("角色名已存在！"));
    } else {
      callback();
    }
  }else{
    callback();
  }
}
const rules=ref({
  rolename:[
    { required: true, message: '请输入角色名'},
    { required: true, validator: checkRolename, trigger: "blur" }
  ],
  code: [{ required: true, message: "请输入权限字符", trigger: "blur" }]
})

const formRef=ref(null)

//初始化form数据
const initFormData=async(id)=>{
  const res = await request.get("sys/role/"+id);
  form.value=res.data.role;
}

watch(
    ()=>props.dialogVisible,
    ()=>{
      let id=props.id;
      console.log("id="+id)
      if(id!=-1){//修改
        initFormData(id);
      }else{//新增
        form.value={
          id:-1,
          rolename:"",
          code:"",
          remark:""
        }
      }
    }
)
//定义父组件的方法
const emits=defineEmits(['update:modelValue','initRoleList'])
//修改父组件的v-model值为false
const handleClose=()=>{
  emits('update:modelValue',false)
}
//提交
const handleConfirm=()=>{
  formRef.value.validate(async(valid)=>{
    if(valid){
      let result=await request.post("sys/role/save",form.value);
      let data=result.data;
      if(data.code==200){
        ElMessage.success("执行成功！")
        formRef.value.resetFields();//重置表单
        emits("initRoleList")//调用父页面传来的方法刷新页面数据
        handleClose();
      }else{
        ElMessage.error(data.msg);
      }
    }else{
      console.log("fail")
    }
  })
}

</script>
<style lang="scss" scoped>
</style>